#include "NGICollection.h"

NGICollection::NGICollection(void) {

}

NGICollection::NGICollection(int _capacity)
{
	this->capacity = _capacity;
	this->nodes = new Node*[capacity];
	count = 0;
}

bool NGICollection::add(Node* n) {
	if(count >= capacity)
		return false;
	nodes[count] = n;
	n->ngi_index = count;
	count++;
	return true;
}

bool NGICollection::remove(int n) {
	if(n >= count)
		return false;
	nodes[n]->ngi_index = -1;
	nodes[n] = nullptr;
	if(count > 1) {
		nodes[n] = nodes[count - 1];
		nodes[n]->ngi_index = n;
	}
	count--;
	return true;
}

bool NGICollection::remove(Node* n) {
	return remove(n->ngi_index);
}

void NGICollection::resize(int newcapacity) {
	capacity = newcapacity;
	if(count == 0) {
		delete[] nodes;
		nodes = new Node*[newcapacity];
		return;
	}

	Node** ns = new Node*[newcapacity];
	for(int i = 0; i < count && i < newcapacity; i++)
		ns[i] = nodes[i];
	if(count > newcapacity)
		count = newcapacity;
	delete nodes;
	nodes = ns;
}

NGICollection::~NGICollection(void)
{
	delete[] nodes;
}
